#!/usr/bin/env python
##############################################################################
##
## Name:         CpuFreq
## Purpose:      Alter laptop cpu max/min frequence and cpu fan work mode
##
## Copyright(C)  2006 lvscar<lvscar@gmail.com>,
##               
## Licence:      GPL version 2 or upper
##
## Created:      August-2006
## Version:      1.0
## 
##############################################################################

import wx
import rwdate

class CpufreqFrame(wx.MDIParentFrame):
	def __init__(self,cpudata):
		wx.MDIParentFrame.__init__(self,None,-1,"CpuFreq")
		MDIChild3 = about(self)
		MDIChild2 = stats(self,cpudata)
		MDIChild1 = fan(self)
		MDIChild0 = freq(self,cpudata)
		self.SetSize(MDIChild0.GetSize()+(10,25))
		
		
		
		

	
class freq(wx.MDIChildFrame):
	def __init__(self,parent,cpudata):
		wx.MDIChildFrame.__init__(self,parent,-1,'freq')
		panel = wx.Panel(self)
		#init input data
		freqlist = self.getfreqlist(cpudata['scaling_available_frequencies'])
		freqlist = [int(x) for x in freqlist]
		freqlist = [x/1000000.0 for x in freqlist]
		freqlist = [str(x)+'GHz' for x in freqlist]
		self.maxfreq = cpudata['scaling_max_freq']
		self.maxfreq = str(int(self.maxfreq)/1000000.0)+'GHz'
		self.minfreq = cpudata['scaling_min_freq']
		self.minfreq = str(int(self.minfreq)/1000000.0)+'GHz'
		#Get elements
		sizer = wx.FlexGridSizer(rows=3, cols=4, hgap=5, vgap=5)
		self.maxtext = wx.StaticText(panel,label="MaxCpuFreq:\n\t%s"%(self.maxfreq))
		self.mintext = wx.StaticText(panel,label="MinCpuFreq:\n\t%s"%(self.minfreq))
		self.maxchoice = wx.Choice(panel, -1, choices=freqlist)
		self.maxchoice.SetStringSelection(self.maxfreq)
		self.minchoice = wx.Choice(panel, -1, choices=freqlist)
		self.minchoice.SetStringSelection(self.minfreq)
		self.button = wx.Button(panel,-1,label="OK")
		self.button.SetDefault()
		self.button.Bind(wx.EVT_BUTTON,self.action)
		#Add elements to sizer
		sizer.Add(self.maxtext)
		sizer.Add((1,1))
		sizer.Add(self.mintext)
		sizer.Add((1,1))
		sizer.Add(wx.StaticText(panel,label="Choose Your\n Max Cpu Freq"))
		sizer.Add(self.maxchoice)
		sizer.Add(wx.StaticText(panel,label="Choose Your\n Min Cpu Freq"))
		sizer.Add(self.minchoice)
		for i in range(3):
			sizer.Add((1,1))
		sizer.Add(self.button)
		#Fit panel ,frame
		panel.SetSizer(sizer)
		panel.Fit()
		self.Fit()
		self.Show()

	def action(self,event):
		max = self.maxchoice.GetStringSelection()
		self.maxtext.SetLabel("MaxCpuFreq:\n\t%s"%(max))
		min = self.minchoice.GetStringSelection()
		self.mintext.SetLabel("MinCpuFreq:\n\t%s"%(min))
		max = self.x1000000(max)
		min = self.x1000000(min)
		#write min big then max is disable, only max been writen,so we write twice
		rwdate.SetCpuData(max,min)
		rwdate.SetCpuData(max,min)
		#print max,min
	def x1000000(self,num):
		snum = str(int(float(num[:-3]) * 1000000))
		return snum
		
	
	def getfreqlist(self,strdata):
		freqlist = strdata.split()
		freqlist = list(set(freqlist))
		freqlist = [int(x) for x in freqlist]
		freqlist.sort()
		freqlist.reverse()
		freqlist = [str(x) for x in freqlist]
		return freqlist


class fan(wx.MDIChildFrame):
	def __init__(self,parent):
		wx.MDIChildFrame.__init__(self,parent,-1,'fan')
		fandata = rwdate.Fandata('get')
		panel = wx.Panel(self)
		sizer = wx.FlexGridSizer(3,3,5,3)
		self.mode = wx.StaticText(panel,-1,fandata[0])
		self.temperature = wx.StaticText(panel,-1,fandata[1])
		self.changemode = '0'
		self.active = wx.RadioButton(panel,-1,'active')
		self.passive = wx.RadioButton(panel,-1,'passive')
		self.Bind(wx.EVT_RADIOBUTTON,self.radio)
		self.button = wx.Button(panel,-1,'OK')
		self.button.Bind(wx.EVT_BUTTON,self.action)
		self.refresh = wx.Button(panel,-1,'Refresh')
		self.refresh.Bind(wx.EVT_BUTTON,self.refresh_action)
		
		sizer.Add(self.mode)
		sizer.Add((1,1))
		sizer.Add(self.temperature)
		sizer.Add(wx.StaticText(panel,-1,'Choose your perfer mode:  '))
		sizer.Add(self.active)
		sizer.Add(self.passive)
		sizer.Add(self.refresh)
		sizer.Add((1,1))
		sizer.Add(self.button)	
		panel.SetSizer(sizer)
		panel.Fit()
		self.Fit()
		self.Show()
		


	def radio(self,event):
		which = event.GetEventObject()
		if which.GetLabel() == 'active':
			self.changemode = '0'
		else : self.changemode = '1'
	
	def action(self,event):
		rwdate.Fandata('set',self.changemode)
		self.updata()	
	
	def refresh_action(self,event):
		self.updata()		
		
	def updata(self):
		fandata = rwdate.Fandata('get')
		self.mode.SetLabel(fandata[0])
		self.temperature.SetLabel(fandata[1])

	
		

class stats(wx.MDIChildFrame):
	def __init__(self,parent,cpudata):
		wx.MDIChildFrame.__init__(self,parent,-1,'stats')
		List = self.count(cpudata['time_in_state'].split())
		text = '\n'.join(List)
		panel = wx.Panel(self)
		text = wx.StaticText(panel,-1,text)
		sizer = wx.FlexGridSizer(1,4,2,10)
		t1 = wx.StaticText(panel,-1,'Total Freq \n Alter Times:' +cpudata['total_trans'])
		t2 = wx.StaticText(panel,-1,'Freq ratio \n  Stats:')
		sizer.Add(t1,flag = wx.ALIGN_CENTER)
		sizer.Add((35,1))
		sizer.Add(t2,flag = wx.ALIGN_CENTER)
		sizer.Add(text)
		panel.SetSizer(sizer)  
		panel.Fit()
		self.Fit()
		self.Show()
	
	def count(self,data):
		amount = 0 
		output = []
		freqs = []
		for i in range(0,len(data),2):
			freqs.append(data[i])
		freqs = [self.hum(i)+'Ghz' for i in freqs]
		for i in range(1,len(data),2):
			amount += int(data[i])
		amount = float(amount)	
		for i in range(1,len(data),2):
			ft = int(data[i]) / amount
			output.append(str(ft)[2:4]+"%")
		for i in range(len(output)):
			output[i] = freqs[i] + ':' + output[i]	
		return output	
	
	def hum(self,n):
		L = list(n[:-5])
		L.insert(-1,'.')
		if L[0] == '.':
			L.insert(0,'0')
		return ''.join(L)	


class about(wx.MDIChildFrame):
	def __init__(self,parent):
		wx.MDIChildFrame.__init__(self,parent,-1,'about',style=wx.DEFAULT_FRAME_STYLE | wx.VSCROLL|wx.HSCROLL)
		panel = wx.ScrolledWindow(self,-1)
		panel.SetScrollbars(1,1,380,200)
		text = """                                    CpuFreq
	tag 'freq'  Alter max/min cpu frequence
	tag 'fan'   Show cpu temperature
			and Change fan work mode
	tag 'stats' Show some cpu frequence statistics


Author:  lvs
Email:   lvscar@gmail.com
Licence:   GPL version 2 or upper
Version:   1.0

	"""
		wx.StaticText(panel,-1,text)
		#panel.Fit()
		#self.Fit()
		self.Show()
		
class wxApp(wx.App):
	def OnInit(self):
		cpudata = rwdate.GetCpuData()
		frame = CpufreqFrame(cpudata)
		frame.Show()

		return True
		
if __name__ == '__main__':
	App = wxApp()
	App.MainLoop()
	
			
		

